package repository

import (
	"errors"
	"gin-shop-admin/dao"
	"gin-shop-admin/models"
	"strconv"
)

func GetOrderList(query string, pageNum string, pageSize string) (orders []models.Order, total int, err error) {
	if pageSize == "" || pageNum == "" {
		return nil, 0, errors.New("参数不正确")
	}
	page_num, _ := strconv.Atoi(pageNum)
	page_size, _ := strconv.Atoi(pageSize)
	if query == "" {
		dao.DB.Model(&models.Order{}).Count(&total)
		if err = dao.DB.Limit(page_size).Offset((page_num - 1) * page_size).Find(&orders).Error; err != nil {
			return nil, total, err
		}
	} else {
		dao.DB.Model(&models.Order{}).Where("order_number like ?", "%"+query+"%").Count(&total)
		if err = dao.DB.Debug().Where("order_number like ?", "%"+query+"%").Limit(page_size).Offset((page_num - 1) * page_size).Find(&orders).Error; err != nil {
			return nil, total, err
		} else {
			if err = dao.DB.Where("order_number like ?", "%"+query+"%").Find(&orders).Count(&total).Error; err != nil {
				return nil, 0, err
			}
		}
	}
	return
}
